using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;

namespace Hoshimi
{
    public class CollectorBot_TransferAZNState : State
	{
		#region Singleton

		// Fields
		protected static CollectorBot_TransferAZNState instance;

		// Constructor
		protected CollectorBot_TransferAZNState() : base() { }

		// Methods
		public static CollectorBot_TransferAZNState Instance()
		{
			// Uses "Lazy initialization"
			if( instance == null ) 
				instance = new CollectorBot_TransferAZNState();

			return instance;
		}

		#endregion

		public override bool Play( Unit unit )
		{
			CollectorBot bot = (CollectorBot) unit;

			bot.SetName("TransfertAZN");

			// We cant give order to the bot. We stop
			if ( bot.State != NanoBotState.WaitingOrders && bot.State != NanoBotState.Moving )
			{
				return true;
			}

			if(bot.IsEmpty())
			{
				// Needle is able to fill the needle before the end of the game?
				if(!bot.IsAbleToFillNeedle())
				{
					// ask his father to remove it
					bot.AskRemove();
				}
				unit.myStateMachine.ChangeState( CollectorBot_MoveToAZNState.Instance() );
				return false;
			}

			// The needle is taken by ennemy
            if (bot.IsMyNeedleTakenByEnemy())
            {
                unit.myStateMachine.ChangeState( CollectorBot_MoveToHoshimiState.Instance() );
                return true;
            }

			// The needle is not assigned : we fill the collector if possible
			if ( !bot.IsMyNeedleAssigned() )
			{
				if ( bot.IsFull() ) 
				{
					bot.MoveToNeedle();
					unit.myStateMachine.ChangeState( CollectorBot_MoveToHoshimiState.Instance() );
					return true;
				}
				
				unit.myStateMachine.ChangeState( CollectorBot_MoveToAZNState.Instance() );
				return true;
			}

			// The needle is full or dead ? We stop
            if (bot.IsMyNeedleFullOrDead())
            {
                return true;
            }

			bot.Transfer();
			return true;
		}
	}
}
